﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.Mvc;

namespace Pgmobile.UI
{
    public static class HtmlHelperExtensions
    {

        public static string BreadCrumb(this HtmlHelper helper, string url)
        {
            StringBuilder html = new StringBuilder();

            string[] paginas = url.Split("/".ToCharArray());

            // Saco los IDs y los strings vacios de la lista
            double temp = 0;
            paginas = paginas.Where(a => !a.Equals("") && !double.TryParse(a, out temp)).ToArray();
            string preUrl = "/";

            for (int i = 0; i < paginas.Length - 1; i++)
            {
                preUrl = preUrl + paginas[i] + "/";
                html.Append("<a href='" + preUrl + "'>" + paginas[i] + "</a> &raquo; ");
            }
            if (paginas.Length > 0)
            {
                html.Append(paginas[paginas.Length - 1]);
            }

            return html.ToString();
        }

        public static string DatePicker(this HtmlHelper helper, string name, string imageUrl, object date)
        {

            StringBuilder html = new StringBuilder();



            // Build our base input element

            html.Append("<input type=\"text\" id=\"" + name + "\" name=\"" + name + "\"");



            // Model Binding Support

            if (date != null)
            {

                string dateValue = String.Empty;



                if (date is DateTime? && ((DateTime)date) != DateTime.MinValue)

                    dateValue = ((DateTime)date).ToShortDateString();

                else if (date is DateTime && (DateTime)date != DateTime.MinValue)

                    dateValue = ((DateTime)date).ToShortDateString();

                else if (date is string)

                    dateValue = (string)date;



                html.Append(" value=\"" + dateValue + "\"");

            }



            // We're hard-coding the width here, a better option would be to pass in html attributes and reflect through them

            // here ( default to 75px width if no style attributes )

            html.Append(" style=\"width: 75px;\" />");



            // Now we call the datepicker function, passing in our options.  Again, a future enhancement would be to

            // pass in date options as a list of attributes ( min dates, day/month/year formats, etc. )

            html.Append("<script type=\"text/javascript\">$(document).ready(function() { $('#" + name + "').datepicker({ showOn: 'button', buttonImage: '" + imageUrl + "', duration: 0 }); });</script>");



            return html.ToString();

        }


        public static void Repeater<T>(this HtmlHelper html
  , IEnumerable<T> items
  , string className
  , string classNameAlt
  , Action<T, string> render)
        {
            if (items == null)
                return;

            int i = 0;

            items.ForEach(item =>
            {
                render(item, (i++ % 2 == 0) ? className : classNameAlt);
            });
        }

        public static void Repeater<T>(this HtmlHelper html
          , string viewDataKey
          , string cssClass
          , string altCssClass
          , Action<T, string> render)
        {
            var items = GetViewDataAsEnumerable<T>(html, viewDataKey);

            int i = 0;
            items.ForEach(item =>
            {
                render(item, (i++ % 2 == 0) ? cssClass : altCssClass);
            });
        }

        static IEnumerable<T> GetViewDataAsEnumerable<T>(HtmlHelper html, string viewDataKey)
        {
            var items = html.ViewContext.ViewData as IEnumerable<T>;
            var viewData = html.ViewContext.ViewData as IDictionary<string, object>;
            if (viewData != null)
            {
                items = viewData[viewDataKey] as IEnumerable<T>;
            }
            else
            {
                //items = new ViewData(viewData)[viewDataKey]
                //  as IEnumerable<T>;
            }
            return items;
        }


        public static void ForEach<T>(this IEnumerable<T> collection, Action<T> action)
        {

            foreach (T item in collection)

                action(item);

        }
    }
}
